<?php
	require_once('Class/evaluation.Class.php');
	require_once('Class/connexionDB.Class.php');

	$db 			= new connexionDB();
    $eval 			= new evaluation();
    
	if (isset($_POST['id_eval']) && !isset($_POST['type']))
    {
    	$result['criteria'] 		     = $eval->getCriteriaForEval($_POST['id_eval']);

        foreach ($result['criteria'] as $key => $value)
        {
            $result['descriptionCriteria'][]   = $db->getLevelMarkDescriptionByIdCriteria($value['id']);
        }
        $result['levelMark'] 		     = $eval->getLevelMark($_POST['id_eval']);
        $result['header'] 			     = $result['levelMark'];
        $result['unacceptable'] 	     = $db->getUnacceptable();

        echo json_encode($result);
    }

    if (isset($_POST['type']) && $_POST['type'] == 'up' && isset($_POST['criteria']) && isset($_POST['level_mark']) && isset($_POST['id_eval']) && isset($_POST['del_header'])  && isset($_POST['del_criteria']))
    {
    	$eval->addEvaluation($_POST['criteria'], $_POST['level_mark'], $_POST['set_to_zero'], $_POST['id_eval'], $_POST['del_header'], $_POST['del_criteria']);
    }

    if (isset($_POST['id_rating']) &&  isset($_POST['type']) && $_POST['type'] == 'delete_id')
    {
        $db->deleteEvaluation($_POST['id_rating']);
    }

    if (isset($_POST['name_eval']) && isset($_POST['type']) && $_POST['type'] == 'check')
    {
        $result = $db->checkIfExist($_POST['name_eval']);

        if (isset($result[0]['number']))
        {
            echo $result[0]['number'];
        }
        else
        {
            echo '0';
        }
    }
    if (isset($_POST['type']) && isset($_POST['id_eval']) && $_POST['type'] == 'delete_eval_rating')
    {
        $db->deleteEvaluationRating(intval($_POST['id_eval']));
    }

    if (isset($_POST['type']) && isset($_POST['id_eval']) && isset($_POST['rename']) && $_POST['type'] == 'rename_eval')
    {
        $db->renameEvalRating(intval($_POST['id_eval']), $_POST['rename']);
    } 
?>